import { get, post } from '@/utils/request'
import { getToken } from '@/utils'
import type { DataInfo, ListResult } from '@/types/request'
//获取公开openToken
const API = '/api/mongoVisualdev/OpenApi/'
//获取公共token
export async function getOpenToken<T>(fn: PromiseCallback<T>) {
  const openToken = getToken()
  if (!openToken) {
    const { msg }: { msg: string } = await get(`${API}getToken/jwsq`)
    uni.setStorageSync('lastLoginTime', String(Date.now()))
    uni.setStorageSync('openToken', msg)
  }
  return fn()
}

// 获取公开数据信息详情
export const getPublicModelInfo = <T>(modelId: string, id: string) =>
  getOpenToken(() => get<DataInfo<T>>(API + `${modelId}/${id}`))

// 获取开放数据列表
export const getPublicModelList = <T>(modeId: string, data = {}) => {
  return getOpenToken(() =>
    post<ListResult<T>>(API + modeId + '/list', {
      pageSize: -1,
      ...data,
    })
  )
}
